package com.vshop.controller.optimize;


import com.alibaba.fastjson.JSON;
import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.callback.request.RequestResultCallback;
import com.vshop.entity.callback.response.ResponseResultCallback;
import com.vshop.entity.purchase.request.RequestPurchaseService;
import com.vshop.entity.purchase.response.ResponsePurchaseService;
import com.vshop.service.alast.OriginalOrderServicePlanService;
import com.vshop.service.alast.UpgradedOrderServicePlanService;
import com.vshop.service.alast.impl.LogicAssistService;
import com.vshop.service.alast.impl.OrderAssistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 去北京异步订购套餐，异步订购结果回调
 *
 * @author lm
 * @date 2020/8/20 14:13
 */

@RestController
@Api(value = "异步订购,结果回调")
@Slf4j
public class AsyncOrderServicePlanController {
    /**
     * 连接数据库的业务处理类
     */
    @Autowired
    LogicAssistService logicAssistService;
    /**
     * 辅助订购套餐的逻辑处理类
     */
    @Autowired
    OrderAssistService assistService;
    /**
     * 升级新版3.0：异步订购和异步回调的业务实现处理类
     */
    @Autowired
    UpgradedOrderServicePlanService upgradedService;
    /**
     * 未升级原版2.0：异步订购和异步回调的业务实现处理类
     */
    @Autowired
    OriginalOrderServicePlanService originalService;

    /**
     * 支付网关请求的 ：订购套餐  接口
     *
     * @param str
     * @return
     */
    @PostMapping(value = "purchaseServicePlan", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "异步续订|新订套餐", response = ResponsePurchaseService.class)
    public String purchaseServicePlan(@RequestBody String str) {
        log.info("【异步续订|新订套餐】请求：" + str);
        if (StringUtils.isBlank(str)) {
            log.info("请求报文为空");
            return assistService.responseStr("9998", "请求报文为空");
        }
        RequestPurchaseService pur;
        try {
            //解析报文：
            pur = JSON.parseObject(str, RequestPurchaseService.class);
        } catch (Exception e) {
            log.info("订购套餐请求报文解析有异常:" + e);
            return assistService.responseStr("9999", "请求报文解析有异常");
        }
        boolean b1 = StringUtils.isBlank(pur.getSecret()) || StringUtils.isBlank(pur.getPartnerCode()) || StringUtils.isBlank(pur.getDeviceCode()) ||
                StringUtils.isBlank(pur.getPackageName()) || StringUtils.isBlank(pur.getPackageCode()) || StringUtils.isBlank(pur.getPackageType()) ||
                StringUtils.isBlank(pur.getOrderPeriod()) || null == pur.getBuyType() || null == pur.getPriceType() ||
                StringUtils.isBlank(pur.getAmount()) || StringUtils.isBlank(pur.getPayType()) || StringUtils.isBlank(pur.getPayRst()) ||
                StringUtils.isBlank(pur.getPayId()) || StringUtils.isBlank(pur.getOrderNo()) || !StringOutputConstant.CODE_SUCCESS.equalsIgnoreCase(pur.getPayRst()) ||
                StringUtils.isBlank(pur.getPayMode());

        /*请求参数验证*/
        if (b1) {
            log.info("请求参数存在不允许为空，不合法的情况");
            return assistService.responseStr("9998", "请求参数不合法");
        }

        /* buyType和priceType同时无值 */
        if (StringUtils.isBlank(pur.getBuyType()) && StringUtils.isBlank(pur.getPriceType())) {
            log.info(pur.getDeviceCode() + "buyType和priceType同时无值");
            return assistService.responseStr("9998", pur.getDeviceCode() + "：buyType和priceType不能同时无值");
        }

        /* buyType和priceType同时有值 */
        if (StringUtils.isNotBlank(pur.getBuyType()) && StringUtils.isNotBlank(pur.getPriceType())) {
            log.info(pur.getDeviceCode() + "buyType和priceType同时有值");
            return assistService.responseStr("9998", pur.getDeviceCode() + "：buyType和priceType不能同时有值");
        }
        /*参数没有问题就开始开始做正事 :Integer c = logicAssistService.queryUpgradedOrNot(pur.getPartnerCode());*/
        /*说明接口从2.0更新到3.0了*/
        return upgradedService.purchaseServicePlan(pur);

    }

    /**
     * 企业网关给的 ：新订|续订的结果回调  接口
     *
     * @param callback
     * @return
     */
    @PostMapping(value = "callback", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "新订|续订的结果回调", response = ResponseResultCallback.class)
    public String orderResultCallback(@ApiParam @RequestBody RequestResultCallback callback) {
        return upgradedService.callback(callback);
    }


}
